/*
 *  This file is part of LifeBudget - Copyright 2010 Clifton Eaton
 *  ------------------------------------------------------------------
 *  LifeBudget is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  LifeBudget is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with LifeBudget.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.lifebudget.app.client.mvc.controller;

import java.util.List;

import org.lifebudget.app.client.events.AppEvents;
import org.lifebudget.app.client.mvc.model.CategoryTotal;
import org.lifebudget.app.client.mvc.view.OverviewView;
import org.lifebudget.app.client.service.TransactionService;
import org.lifebudget.app.client.service.TransactionServiceAsync;

import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Controller;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;

/**
 * Controller class for Overview related events and supporting
 * the Overview view.
 */
public class OverviewController extends Controller {

	/** The overview view. */
	private OverviewView overviewView;
	
	/** The transaction service. */
	private TransactionServiceAsync transactionService;

	/**
	 * Instantiates a new overview controller and registers all events 
	 * this controller can handle.
	 */
	public OverviewController() {
		registerEventTypes(AppEvents.Init);
		registerEventTypes(AppEvents.RefreshCategory);
		registerEventTypes(AppEvents.RefreshTransaction);
	}

	/* (non-Javadoc)
	 * @see com.extjs.gxt.ui.client.mvc.Controller#initialize()
	 */
	public void initialize() {
		overviewView = new OverviewView(this);
		transactionService = GWT.create(TransactionService.class);
	}

	/* (non-Javadoc)
	 * @see com.extjs.gxt.ui.client.mvc.Controller#handleEvent(com.extjs.gxt.ui.client.mvc.AppEvent)
	 */
	@Override
	public void handleEvent(AppEvent event) {
		if (event.getType() == AppEvents.Init) {
			onInit(event);
		}
		if (event.getType() == AppEvents.RefreshCategory || event.getType() == AppEvents.RefreshTransaction) {
			onRefresh(event);
		}
	}

	/**
	 * Handles the <code>RefreshCategory</code> and <code>RefreshTransaction</code> events, 
	 * invoking the transaction service to update the Category Totals and then forwarding the events
	 * and data to the overview view.
	 *
	 * @param event either the <code>RefreshCategory</code> event or the <code>RefreshTransaction</code>
	 * 				event.
	 */
	private void onRefresh(final AppEvent event) {
		// GWT.log("OverviewController received AppEvent Init, forwarding to view");
		transactionService.getCategoryTotals(new AsyncCallback<List<CategoryTotal>>() {

			@Override
			public void onSuccess(List<CategoryTotal> result) {
				event.setData(result);
				forwardToView(overviewView, event);
			}

			@Override
			public void onFailure(Throwable caught) {
				Dispatcher.get().dispatch(AppEvents.Error);

			}
		});
	}

	/**
	 * Handles the Init event, calling the service to get the totals and then 
	 * forwarding to the Overview view.
	 *
	 * @param event the Init event
	 */
	private void onInit(final AppEvent event) {
		// GWT.log("OverviewController received AppEvent Init, forwarding to view");
		transactionService.getCategoryTotals(new AsyncCallback<List<CategoryTotal>>() {

			@Override
			public void onSuccess(List<CategoryTotal> result) {
				event.setData(result);
				forwardToView(overviewView, event);
			}

			@Override
			public void onFailure(Throwable caught) {
				Dispatcher.get().dispatch(AppEvents.Error);

			}
		});

	}
}
